package borge.web.teamspirit.client.data;

import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.UserContentProvider;

/**
 * Database object.
 * Representation of a player mapped to a team.
 * @author Andreas Borglin
 *
 */
@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class TeamPlayer implements PersistentData {

	/* Encoded key generated by GAE when first saved.
	 * Guaranteed to be unique and contains mapping to 
	 * a team using gae.pk-name key defined below. */
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String iEncodedKey;
    
	@Persistent
	private String iTeamKey;

    @Persistent
    private String iPersonKey;
    
    // Redundant name info for performance
    @Persistent
    private String iPersonName;
    
	// Team related

    @Persistent
    protected Boolean iActive;
    
	@Persistent
	private String iPosition;
	
	@Persistent
	private String iShirtNumber;
	
	@Persistent
	private Date iJoinDate;
	
	@Persistent
	private String iOtherInfo;
	
	//@Persistent
	//private String[] iImagePaths;
	
	public static TeamPlayer createPlayerFromUserForm(TeamPlayer aPlayer, UserContentProvider aProvider){
	    
	    if(aPlayer == null)
	        aPlayer = new TeamPlayer(); 
	    
	    aPlayer.setActive(aProvider.getBoolean(TeamSpirit.getConstants().active()));
	    
	    aPlayer.setShirtNumber(aProvider.getString(TeamSpirit.getConstants().shirt_number()));
	    aPlayer.setJoinDate(aProvider.getDate(TeamSpirit.getConstants().join_date()));
	    aPlayer.setPosition(aProvider.getString(TeamSpirit.getConstants().position()));
	    aPlayer.setOtherInfo(aProvider.getString(TeamSpirit.getConstants().other_info()));
	    
	    return aPlayer;
	}
	
	public TeamPlayer(){
		
		// for testing
		iActive = true;
		iJoinDate = new Date();
		
	}
	
	@Override
	public String toString(){
	    return iPersonName;
	}
	
	public void setPersonName(String aName){
		iPersonName = aName;
	}
	
	public String getPersonName(){
		return iPersonName;
	}
	
	public void setTeamKey(String aKey){
		iTeamKey = aKey;
	}
	
	public String getTeamKey(){
		return iTeamKey;
	}
	
	public void setPersonKey(String aKey){
		iPersonKey = aKey;
	}
	
	public String getPersonKey(){
		return iPersonKey;
	}
	
	public String getEncodedKey(){
		return iEncodedKey;
	}
	
    public void setPosition(String aPosition) {
        this.iPosition = aPosition;
    }

    public String getPosition() {
        return iPosition;
    }

    public void setShirtNumber(String aShirtNumber) {
        this.iShirtNumber = aShirtNumber;
    }

    public String getShirtNumber() {
        return iShirtNumber;
    }

    public void setJoinDate(Date aJoinDate) {
        this.iJoinDate = aJoinDate;
    }

    public Date getJoinDate() {
        return iJoinDate;
    }

    public void setOtherInfo(String aOtherInfo) {
        this.iOtherInfo = aOtherInfo;
    }

    public String getOtherInfo() {
        return iOtherInfo;
    }

    public void setActive(Boolean aActive) {
        this.iActive = aActive;
    }

    public Boolean isActive() {
        return iActive;
    }

    /*
    public void setPlayerId(Long aPlayerId) {
        this.iPlayerId = aPlayerId;
    }

    public Long getPlayerId() {
        return iPlayerId;
    }*/
	
	@Override
	public String getDisplayingWidget() {
		// TODO Auto-generated method stub
		return null;
	}
    
    public String dumpContent(){
        return new String("Dump: " + 
                "\nId:" + iEncodedKey + 
                "\nActive:" + iActive + 
                "\nPosition:" + iPosition + 
                "\nShirt number:" + iShirtNumber + 
                "\nJoin date:" + iJoinDate +
                "\nOther info:" + iOtherInfo);
    }
	
}
